package com.xczl.demo.action.front;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xczl.demo.domain.User;
import com.xczl.demo.service.UserService;

@Controller
public class LoginAction {
	/**
	 * 用户服务
	 */
	@Autowired
	private UserService userService;
	/**
	 * 访问注册页
	 * @return
	 */
	@RequestMapping("/regPage.htm")
	public String regPage() {
		System.out.println("用户注册");
		return "front/reg";
	}
	
	/**
	 * 注册
	 * @param 
	 * @return
	 */
	@RequestMapping("/reg.htm")
	public String reg(User user) {
		userService.addUser(user);
		return "redirect:index.htm";
	} 
	
	@RequestMapping("/loginPage.htm")
	public String loginPage() {
		//System.out.println("用户登录");
		return "front/login";
	}
	

	/**
	 * 登录页
	 * @return
	 */
	@RequestMapping("/login.htm")
	public String login(String username, String password,HttpServletRequest request) {
		User user = userService.login(username, password);
		if(user == null){
			return "redirect:loginFial.htm";
		}
		request.getSession().setAttribute(User.SESSION_ID, user);
		return "redirect:index1.htm";
		
	}

	@RequestMapping("/loginFial.htm")
	public String loginFial(){
		return "front/loginFial";
	}
}
